﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using TomNet.IMSC.Core.Contracts.Identity;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.ViewModels;
using TomNet.Web.Mvc;

namespace TomNet.IMSC.Web.Common
{
    public class CookiesManagement
    {
        public static HttpCookie GetCookies()
        {
            try
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                return HttpContext.Current.Request.Cookies[cookieName];
            }
            catch
            {
                return null;
            }

        }

        public static FormsAuthenticationTicket GetTicket()
        {
            try
            {
                var cookies = GetCookies();
                return cookies == null ? null : FormsAuthentication.Decrypt(cookies.Value);
            }
            catch
            {
                return null;
            }
        }
        public static FormsAuthenticationTicket GetTicket(HttpCookie cookies)
        {
            try
            {
                return FormsAuthentication.Decrypt(cookies.Value);
            }
            catch
            {
                return null;
            }
        }
        public static LoginModel GetLoginModel(FormsAuthenticationTicket ticket)
        {
            try
            {
                return JsonConvert.DeserializeObject<LoginModel>(ticket.UserData);
            }
            catch
            {
                return null;
            }
        }

        public static void RefreshCookies(FormsAuthenticationTicket ticket, LoginModel model = null)
        {

            var cookies = GetCookies();
            if (cookies == null) return;
            if (ticket.IsPersistent) return;
            string userData = model == null ? "" : JsonConvert.SerializeObject(model);

            var nticket = new FormsAuthenticationTicket(
                                        1,
                                        ticket.Name,
                                        DateTime.Now,
                                        DateTime.Now.Add(FormsAuthentication.Timeout),
                                        ticket.IsPersistent,
                                        userData == "" ? ticket.UserData : userData);

            cookies.Value = FormsAuthentication.Encrypt(nticket);
            HttpContext.Current.Response.Cookies.Set(cookies);

        }

        public static IRoleAuthorityContract RoleAuthorityContract { get; set; }
        public static IStaffRolesContract StaffRolesContract { get; set; }
        public static IStaffAuthorityContract StaffAuthorityContract { get; set; }

        public static LoginModel GetAutorModel()
        {            
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            var model = Common.CookiesManagement.GetLoginModel(ticket);
            
            List<int> al = new List<int>();
            var qa = from a in RoleAuthorityContract.Entities
                     join
                         b in StaffRolesContract.Entities on a.RoleID equals b.RoleID
                     where b.StaffID == model.Id
                     select a.AuthorityID;
            al.AddRange(qa.ToList());
            var sa = from a in StaffAuthorityContract.Entities
                     where a.StaffID == model.Id
                     select a.AuthorityID;
            al.AddRange(sa.ToList());

            model.AuthorityIds = string.Join(",", (from a in al select (a)).Distinct().ToList());


            return model;
        }
    }
}